package com.example.composestudy.router.part1.home

import android.os.Bundle
import android.os.Parcelable
import androidx.navigation.NamedNavArgument
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavType
import androidx.navigation.navArgument
import com.example.composestudy.router.RouterConfig
import com.google.gson.Gson
import kotlinx.parcelize.Parcelize

object HomeDirection {
    @Parcelize
    data class HomeArgs(
        val name: String,
        val age: Int,
    ) : Parcelable

    const val route = "${RouterConfig.HOME}?" +
            "${RouterConfig.HOME_ARG_NAME}={${RouterConfig.HOME_ARG_NAME}}&" +
            "${RouterConfig.HOME_ARG_AGE}={${RouterConfig.HOME_ARG_AGE}}"

    val arguments: List<NamedNavArgument> = listOf(
        navArgument(RouterConfig.HOME_ARG_NAME) {
            type = NavType.StringType
            // 可选参数具有以下两个值之一，要么有默认值，要么是可空的
            nullable = true
            //defaultValue = "未知用户"
        },
        navArgument(RouterConfig.HOME_ARG_AGE) {
            type = NavType.IntType
            defaultValue = 0
        },
    )

    fun parsArguments(entry: NavBackStackEntry): HomeArgs {
        return HomeArgs(
            name = entry.arguments?.getString(RouterConfig.HOME_ARG_NAME) ?: "未知用户",
            age = entry.arguments?.getInt(RouterConfig.HOME_ARG_AGE)!!,
        )
    }

    fun toHomePager(name: String, age: Int?): String {
        return "${RouterConfig.HOME}?" +
                "${RouterConfig.HOME_ARG_NAME}=$name&" +
                "${RouterConfig.HOME_ARG_AGE}=$age"
    }
}